package com.stanley.alogrithm.java;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

public class Java8Main {
    public static void main(String[] args) {
        MathOperation mathOperation = (int a, int b) -> a + b;

        Car car1 = Car.create(new Supplier<Car>() {
            @Override
            public Car get() {
                return new Car();
            }
        });

        Car car2 = Car.create(Car::new);
        List<Car> carList = Arrays.asList(car1, car2);
        carList.forEach(Car::collide);
        carList.forEach(Car::repair);
        carList.forEach(car2::follow);
    }

    interface  MathOperation {
        int op(int a, int b);
    }

    static class Car {
        //Supplier是jdk1.8的接口，这里和lamda一起使用了
        public static Car create(final Supplier<Car> supplier) {
            return supplier.get();
        }

        public static void collide(final Car car) {
            System.out.println("Collided " + car.toString());
        }

        public static void collide2(final Car car1, final Car car2) {
            System.out.println("Collided " + car1.toString() + " with " + car2.toString());
        }

        public void follow(final Car another) {
            System.out.println("Following the " + another.toString());
        }

        public void repair() {
            System.out.println("Repaired " + this.toString());
        }
    }
}
